﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyModels
{
    /// <summary>
    /// 通用分页信息类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PageModel<T>
    {
        /// <summary>
        /// 当前页标
        /// </summary>
        public int Page { get; set; } = 1;
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount => (int)Math.Ceiling((decimal)DataCount / PageSize);
        /// <summary>
        /// 数据总数
        /// </summary>
        public int DataCount { get; set; } = 0;
        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; set; } = 20;
        /// <summary>
        /// 返回数据
        /// </summary>
        public List<T> Data { get; set; }
        public PageModel() { }

        public PageModel(int page, int dataCount, int pageSize, List<T> data)
        {
            Page = page;
            Data = data;
            DataCount = dataCount;
            PageSize = pageSize;
        }

        public PageModel<TOut> ConvertTo<TOut>()
        {
            return new PageModel<TOut>(Page, DataCount, PageSize, default);
        }

        public PageModel<TOut> ConvertTo<TOut>(IMapper mapper)
        {
            var model = ConvertTo<TOut>();
            if (Data != null) model.Data = mapper.Map<List<TOut>>(Data);
            return model;
        }

        public PageModel<TOut> ConvertTo<TOut>(IMapper mapper, Action<IMappingOperationOptions> options)
        {
            var model = ConvertTo<TOut>();
            if (Data != null) model.Data = mapper.Map<List<TOut>>(Data, options);
            return model;
        }
    }
}
